<template>
  <div class="login_container" id="login">
    <div class="login_box">
      <!-- 头像区 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 表单区 -->
      <el-form
        ref="loginFormRef"
        :model="login_from"
        :rules="loginFromRules"
        label-width="0px"
        class="login_from"
      >
        <!-- 登陸名 -->
        <el-form-item prop="username">
          <el-input
            v-model="login_from.username"
            placeholder="请输入用戶名"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密碼區 -->
        <el-form-item prop="password">
          <el-input 
            v-model="login_from.password"
            placeholder="请输入密碼"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按鈕區 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登錄</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    // 這是登錄表單的數據綁定對象
    return {
      login_from: {
        username: "admin",
        password: "123456",
      },
    //   這是表單的驗證規則對象
      loginFromRules: {
        name: [
          { required: true, message: "请输入登錄名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入您的密碼", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
      resetLoginForm() {
          return this.$refs.loginFormRef.resetFields();
      },
      login() {
           return this.$refs.loginFormRef.validate(async valid=>{
            //    console.log(valid)
            if(!valid) return;
            const {data: result}=await  this.$http.post("login",this.login_from)
            
            if(result.meta.status!==200) return this.$Message.error("登录失败");
            this.$Message.success("登录成功");
            window.sessionStorage.setItem("token",result.data.token)
            this.$router.push("/home")
            // console.log(result)
            // console.log(window)
           });
      }
  }
};
</script>

<style Lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  border-radius: 6px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  position: absolute;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}
.avatar_box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.login_from {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>